<?php
namespace app\member\model;

use app\BaseModel;
use app\member\info\PermissionMember;
use think\facade\Db;

class DiscountGoods extends BaseModel
{
    //会员商品表
    protected $name = 'longbing_card_member_discount_goods';



    /**
     * @author chenniang
     * @DataTime: 2020-07-15 09:49
     * @功能说明:添加
     */
    public function goodsAdd($data,$uniacid,$goods_id){

        $this->where(['goods_id'=>$goods_id])->delete();

        foreach ($data as $k=>$v){

            $data[$k]['uniacid'] = $uniacid;

            $data[$k]['goods_id'] = $goods_id;

        }

        $res = $this->saveAll($data);

        return $res;
    }


    /**
     * @param $goods_id
     * @功能说明:商品折扣
     * @author chenniang
     * @DataTime: 2020-07-29 15:10
     */

    public function goodsDiscount($goods_id){

        $dis = [

            'goods_id' => $goods_id
        ];

        $data = $this->where($dis)->select()->toArray();

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-30 17:06
     * @功能说明:用户会员的折扣
     */
    public function userGoodsDiscount($goods_id,$user_id,$uniacid){

        $config_model = new Config();

        $config = $config_model->configInfo(['uniacid'=>$uniacid]);

        $level_model = new Level();
        //会员当前的等级
        $user_level  = $level_model->getUserLevel($user_id,$uniacid);

        $member_id   = !empty($user_level)?$user_level['id']:0;

        $dis = [

            'goods_id' => $goods_id,

            'member_id'=> $member_id
        ];

        $data = $this->where($dis)->find();
        //折扣
        $discount = !empty($data)&&$config['status']==1?$data['discount']/10:1;

        return $discount;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-08-03 17:39
     * @功能说明:是不是会员商品
     */
    public function isMemberGoods($goods_id,$user_id,$uniacid){

        $config_model = new Config();

        $config = $config_model->configInfo(['uniacid'=>$uniacid]);

        $level_model = new Level();
        //会员当前的等级
        $user_level  = $level_model->getUserLevel($user_id,$uniacid);

        $member_id   = !empty($user_level)?$user_level['id']:0;

        $dis = [

            'goods_id' => $goods_id,

            'member_id'=> $member_id
        ];

        $data = $this->where($dis)->find();
        //折扣
        $data = !empty($data)&&$config['status']==1?1:0;

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-08-10 11:45
     * @功能说明:会员商品
     */
    public function isMember($is_member,$uniacid){

        $config_model = new Config();

        $config = $config_model->configInfo(['uniacid'=>$uniacid]);

        $persisson = new PermissionMember($uniacid);

        $auth = $persisson->pAuth();

        return $auth==true&&$config['status']==1?$is_member:0;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-02-01 10:57
     * @功能说明:会员折扣
     */
    public function eventDiscount($where){
        //会员商品折扣
        $data['discount']        = $this->userGoodsDiscount($where['goods_id'],$where['user_id'],$where['uniacid']);
        //是不是会员折扣商品
        $data['is_goods_member'] = $this->isMemberGoods($where['goods_id'],$where['user_id'],$where['uniacid']);

        $data['name']            = 'member_discount';

        $data['member_discount'] = $data['discount'];

        return $data;
    }












}